/*-----------------------------------------------------------------------------------
 *
 * 프로그램명	: ProductDAO.java
 * Version		: 1.0
 * 작성일 		: 2006-08-16
 * 작성자		:
 * 수정일 		:
 * 수정자		:
 * 설명		 	:
 * 기능		 	:
 *
 * Copyright (c) 2000 by H&H  All Rights Reserved.
 *
-------------------------------------------------------------------------------------*/
package com.joinhawaii.home;

import java.io.*;


import java.util.*;
import java.sql.*;

import com.zll.framework.conf.Configuration;
import com.zll.framework.db.DBManager;
import com.zll.framework.model.HashBox;
import com.zll.framework.model.HashList;
import com.zll.framework.util.StringUtil;



public class HomeDAO
{
	/* 로그인 */
	public HashList selectReqCustomer(HashMap param) throws Exception
	{
        return DBManager.executeQuery("/home.xml", "select_req_customer", param);
	}


	/* 메뉴 */
	public HashList selectTopMenuList() throws Exception
	{
        return DBManager.executeQuery("/home.xml", "select_top_menu_list", new HashMap());
	}
	public HashList selectLeftMenuList(String pMenuId) throws Exception
	{
		HashMap infos = new HashMap();
		infos.put("pmenu_id", pMenuId);
        return DBManager.executeQuery("/home.xml", "select_left_menu_list", infos);
	}
	public HashList selectPackageMenuList() throws Exception
	{
        return DBManager.executeQuery("/home.xml", "select_package_menu_list", new HashMap());
	}
	public HashList selectOptionMenuList() throws Exception
	{
        return DBManager.executeQuery("/home.xml", "select_option_menu_list", new HashMap());
	}
	public HashList selectGolfMenuList() throws Exception
	{
        return DBManager.executeQuery("/home.xml", "select_golf_menu_list", new HashMap());
	}

	/* 메인 공지사항 */
	public HashList selectWordList(HashMap param) throws Exception {
        return DBManager.executeQuery("/home.xml", "select_word_list", param);
	}


	/* 패키지 */
	public HashList selectPackageList(HashMap param) throws Exception {
        return DBManager.executeQuery("/home.xml", "select_package_list", param);
	}
	public HashList selectPackageScheduleList(HashMap param) throws Exception {
        return DBManager.executeQuery("/home.xml", "select_package_schedule_list", param);
	}


	/* 호텔 */
	public HashList selectHotelList(HashMap param) throws Exception {
        return DBManager.executeQuery("/home.xml", "select_hotel_list", param);
	}
	public HashList selectRoomPriceList(HashMap param) throws Exception {
        return DBManager.executeQuery("/home.xml", "select_room_price_list", param);
	}
	public String selectMinPriceList(String hotel_id) throws Exception {
		HashMap param = new HashMap();
		param.put("hotel_id", hotel_id);
        return DBManager.executeQuery("/home.xml", "select_hotel_min_sales_price", param).get("min_sales_price", 0);
	}

	/* 선택관광 */
	public HashList selectEnjoyList(HashMap param) throws Exception {
        return DBManager.executeQuery("/home.xml", "select_enjoy_list", param);
	}
	public HashList selectEnjoyDetail(HashMap param) throws Exception {
        return DBManager.executeQuery("/home.xml", "select_enjoy_detail", param);
	}

	/* 렌터카 */
	public HashList selectCarList(HashMap param) throws Exception {
        return DBManager.executeQuery("/home.xml", "select_car_list", param);
	}

	/* 항공 */
	public HashList selectAir(HashMap param) throws Exception {
        return DBManager.executeQuery("/home.xml", "select_air", param);
	}
	public HashList selectAirList(HashMap param) throws Exception {
        return DBManager.executeQuery("/home.xml", "select_air_list", param);
	}

	/*주내선*/
	public HashList selectFlyList(HashMap param) throws Exception {
        return DBManager.executeQuery("/home.xml", "select_fly_list", param);
	}


	/* 골프 */
	public HashList selectGolfList(HashMap param) throws Exception {
        return DBManager.executeQuery("/home.xml", "select_golf_list", param);
	}

	/* 페이지 */
	public HashList selectPageList(HashMap param) throws Exception {
        return DBManager.executeQuery("/home.xml", "select_page_list", param);
	}


	/* 견적의뢰 */
	public HashList selectReqList(HashMap infos) throws Exception
	{
		StringUtil.setPaging("/home.xml", "select_req_total", infos);
        return DBManager.executeQuery("/home.xml", "select_req_list", infos);
	}

	public int selectReqCount(HashMap infos) throws Exception
	{
        HashList list = DBManager.executeQuery("/home.xml", "select_req_count", infos);
		return Integer.parseInt(list.get("req_count",0));
	}


	/* 견적서 */
	public HashList selectEstList(HashMap param) throws Exception {
        return DBManager.executeQuery("/home.xml", "select_est_list", param);
	}

	/* 예약확인서 */
	public HashList selectConfirmList(HashMap param) throws Exception {
        return DBManager.executeQuery("/home.xml", "select_confirm_list", param);
	}

	/**
	 * 견적의뢰 저장
	 *
	 * @param	infos
	 * @return	boolean
	 */
	public void saveReq(HashBox box) throws Exception
    {
		Connection conn = null;
        //boolean result	= true;

        try
		{
			conn = DBManager.getConnection();
			HashMap map = box.getParam();

	    	if ("C".equals(map.get("job_type"))) {
	    		DBManager.executeUpdate("/home.xml", "insert_req", map, conn);
	       	}
	    	else if ("U".equals(map.get("job_type"))) {
	    		DBManager.executeUpdate("/home.xml", "update_req", map, conn);
	       	}
	    	else if ("D".equals(map.get("job_type"))) {
	    		DBManager.executeUpdate("/home.xml", "delete_req", map, conn);
	       	}

			conn.commit();
	    }
	    catch(Exception e)
	    {
	    	DBManager.rollback(conn);
	    	throw e;
		}
	    finally
	    {
	    	DBManager.closeConnection(conn);
		}
    }



	/**
	 * 바로예약 저장
	 *
	 * @param	infos
	 * @return	boolean
	 */
	public void insertReserve(HashBox box) throws Exception
    {
		Connection conn = null;
        //boolean result	= true;

        try
		{
			conn = DBManager.getConnection();
			HashMap map = box.getParam();

			String reqId = DBManager.executeQuery("/home.xml", "select_req_id", map, conn).get("req_id", 0);

			String travelEtc = "";
			int enjoyCount = 0;
			int carCount = 0;

			//호텔
			HashList hotel 	= box.getList("hotel");
			for (int i = 0; i < hotel.size(); i++) {
				HashMap infos = (HashMap)hotel.get(i);

				if (!"".equals(infos.get("hotel_name"))) {
					infos.put("req_id", reqId);
					DBManager.executeUpdate("/home.xml", "insert_req_hotel", infos, conn);
				}
			}

			//여행자
			HashList customer 	= box.getList("customer");
			for (int i = 0; i < customer.size(); i++) {
				HashMap infos = (HashMap)customer.get(i);

				if (!"".equals(infos.get("customer_name"))) {
					infos.put("req_id", reqId);
					DBManager.executeUpdate("/home.xml", "insert_req_customer", infos, conn);
				}
			}


			//선택관광
			HashList enjoy 	= box.getList("enjoy");
			for (int i = 0; i < enjoy.size(); i++) {
				HashMap infos = (HashMap)enjoy.get(i);

				if (!"".equals(infos.get("enjoy_id"))) {
					infos.put("req_id", reqId);
					DBManager.executeUpdate("/home.xml", "insert_req_enjoy", infos, conn);
					enjoyCount++;
				}
			}

			//렌터카
			HashList car	= box.getList("car");
			for (int i = 0; i < car.size(); i++) {
				HashMap infos = (HashMap)car.get(i);

				if (!"".equals(infos.get("car_id"))) {
					infos.put("req_id", reqId);
					DBManager.executeUpdate("/home.xml", "insert_req_car", infos, conn);
					carCount++;
				}
			}



			if (enjoyCount > 0) {
				travelEtc += "선택관광";
				if (carCount > 0) {
					travelEtc += "+렌터카";
				}
			}
			else {
				if (carCount > 0) {
					travelEtc += "렌터카";
				}
			}


			map.put("req_id", reqId);
			map.put("travel_etc", travelEtc);
	    	DBManager.executeUpdate("/home.xml", "insert_reserve", map, conn);


			conn.commit();
	    }
	    catch(Exception e)
	    {
	    	DBManager.rollback(conn);
	    	throw e;
		}
	    finally
	    {
	    	DBManager.closeConnection(conn);
		}
    }


	/**
	 * 바로예약 수정
	 *
	 * @param	infos
	 * @return	boolean
	 */
	public void saveReserve(HashBox box) throws Exception
    {
		Connection conn = null;
        //boolean result	= true;

        try
		{
			conn = DBManager.getConnection();
			HashMap map = box.getParam();


			String travelEtc = "";
			int enjoyCount = 0;
			int carCount = 0;

			//호텔
			HashList hotel 	= box.getList("hotel");

			DBManager.executeUpdate("/home.xml", "delete_req_hotel_all", map, conn);
			for (int i = 0; i < hotel.size(); i++) {
				HashMap infos = (HashMap)hotel.get(i);
				if (!"".equals(infos.get("hotel_name"))) {
					infos.put("req_id", map.get("req_id"));
					DBManager.executeUpdate("/home.xml", "insert_req_hotel", infos, conn);
				}
			}

			//여행자
			HashList customer 	= box.getList("customer");
			DBManager.executeUpdate("/home.xml", "delete_req_customer_all", map, conn);
			for (int i = 0; i < customer.size(); i++) {
				HashMap infos = (HashMap)customer.get(i);
				if (!"".equals(infos.get("customer_name"))) {
					infos.put("req_id", map.get("req_id"));
					DBManager.executeUpdate("/home.xml", "insert_req_customer", infos, conn);
				}
			}


			//선택관광
			HashList enjoy 	= box.getList("enjoy");
			for (int i = 0; i < enjoy.size(); i++) {
				HashMap infos = (HashMap)enjoy.get(i);
				if (!"".equals(infos.get("enjoy_id"))) {
					infos.put("req_id", map.get("req_id"));
					DBManager.executeUpdate("/home.xml", "insert_req_enjoy", infos, conn);
					enjoyCount++;
				}
			}

			//렌터카
			HashList car	= box.getList("car");
			for (int i = 0; i < car.size(); i++) {
				HashMap infos = (HashMap)car.get(i);
				if (!"".equals(infos.get("car_id"))) {
					infos.put("req_id", map.get("req_id"));
					DBManager.executeUpdate("/home.xml", "insert_req_car", infos, conn);
					carCount++;
				}
			}



			if (enjoyCount > 0) {
				travelEtc += "선택관광";
				if (carCount > 0) {
					travelEtc += "+렌터카";
				}
			}
			else {
				if (carCount > 0) {
					travelEtc += "렌터카";
				}
			}


			map.put("travel_etc", travelEtc);
	    	DBManager.executeUpdate("/home.xml", "update_reserve", map, conn);


			conn.commit();
	    }
	    catch(Exception e)
	    {
	    	DBManager.rollback(conn);
	    	throw e;
		}
	    finally
	    {
	    	DBManager.closeConnection(conn);
		}
    }

	/**
	 * 바로예약 삭제
	 *
	 * @param	infos
	 * @return	boolean
	 */
	public void deleteReserve(HashBox box) throws Exception
    {
		Connection conn = null;
        //boolean result	= true;

        try
		{
			conn = DBManager.getConnection();
			HashMap map = box.getParam();

	    	DBManager.executeUpdate("/home.xml", "delete_req", map, conn);

			conn.commit();
	    }
	    catch(Exception e)
	    {
	    	DBManager.rollback(conn);
	    	throw e;
		}
	    finally
	    {
	    	DBManager.closeConnection(conn);
		}
    }

}
